<!-- 蓝色登录页面2 -->
<template>
  <view style="height:100vh;background: #fff;">
    <view class="img-a">
      <view class="t-b">
        您好，
        <br/>
        欢迎使用,可爱妙妙屋!
      </view>
    </view>
    <view class="login-view" style="">
      <view class="t-login">
        <form class="cl">
          <view class="t-a">
            <text class="txt">手机号</text>
            <input v-model="form.telephone" maxlength="11" name="phone" placeholder="请输入您的手机号" type="number"/>
          </view>
          <view class="t-a">
            <text class="txt">密码</text>
            <input v-model="form.password" maxlength="18" name="code" placeholder="请输入您的密码" type="password"/>
          </view>
          <view class="t-a" @onclick="yanzhengma">
            <text class="txt">验证码</text>
            <view style="display: flex;justify-content: space-between;width: 100%;">
              <input v-model="form.code" maxlength="5" name="code" placeholder="请输入您的验证码" type="text"/>
              <image :src="image" alt="验证码" style="height:90rpx;width:228rpx" @click="yanzhengma"/>
            </view>
          </view>
          <button @tap="login">登 录</button>
          <view class="reg" @tap="reg()">注 册</view>
        </form>
        <view class="t-f">
          <text>—————— 第三方账号登录 ——————</text>
        </view>
        <view class="t-e cl">
          <view class="t-g">
            <image src="https://zhoukaiwen.com/img/loginImg/wx.png"></image>
          </view>
          <view class="t-g" @tap="zfbLogin()">
            <image src="https://zhoukaiwen.com/img/loginImg/qq.png"></image>
          </view>
        </view>
      </view>
    </view>
  </view>
  <!--  <view-->
  <!--      style="position:absolute;z-index: 100;top: 40%;left: 35%;"-->
  <!--  >-->
  <!--    <up-loading-icon :show="shows" size="28"-->
  <!--                     text="登录中"-->
  <!--                     textSize="28"></up-loading-icon>-->
  <!--  </view>-->
  <up-toast ref="uToastRef"></up-toast>
</template>
<script setup>
import {ref} from 'vue';
import {getYanZengAPI, loginAPI} from '../../api/login.js'
import {onShow} from "@dcloudio/uni-app";

const image = ref("")
const key = ref("")
const uToastRef = ref(null)
/**
 * 验证码异步方法
 * @returns {Promise<void>}
 */
const yanzhengma = async function yanzhengma() {
  const res = await getYanZengAPI()
  console.log(res);
  if (res.code == 200) {
    image.value = res.data.image
    key.value = res.data.key
  } else {
    uni.showToast({title: res.msg, icon: 'none'});
  }

}

onShow(async () => {
  await yanzhengma();
})

const shows = ref(false);
const loadingText = ref('正在登录');
const form = ref({
  telephone: '',
  password: '',
  code: '',
  key: ''
})

const role = () => {
  if (!form.value.telephone) {
    // uni.showToast({title: '请输入您的手机号', icon: 'none'});
    uToastRef.value.show({message: "请输入您的密码", type: 'error', position: "top", duration: 1000});
    return false;
  }
  if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(form.value.telephone)) {
    // uni.showToast({title: '请输入正确手机号', icon: 'none'});
    uToastRef.value.show({message: "请输入正确手机号", type: 'error', position: "top", duration: 1000});
    return flase;
  }
  if (!form.value.password) {
    // uni.showToast({title: '请输入您的密码', icon: 'none'});
    uToastRef.value.show({message: "请输入您的密码", type: 'error', position: "top", duration: 1000});
    return false;
  }
  if (!form.value.code) {
    // uni.showToast({title: '请输入验证码', icon: 'none'});
    uToastRef.value.show({message: "请输入验证码", type: 'error', position: "top", duration: 1000});
    return false;
  }
  return true;
}

const login = async () => {
  if (!role()) {
    // 如果role函数返回false，则不发送请求，直接返回
    return;
  }
  // shows.value = true
  await uToastRef.value.show({message: "正在登录", type: 'loading', duration: 2500});
  form.value.key = key.value
  const res = await loginAPI(form.value);
  console.log(res, 'res')
  if (res.code === 200) {
    // 等待一秒后跳转
    // setTimeout(() => {
    // 隐藏加载提示
    // shows.value = false;
    // 存储 tokenName 和 tokenValue 到本地存储
    uni.setStorageSync('tokenName', res.data.tokenName);
    uni.setStorageSync('tokenValue', res.data.tokenValue);
    // 跳转到其他页面，例如 'home'
    await uni.switchTab({url: '/pages/index/index'});
    // }, 1000);
  } else {
    await yanzhengma();
    // 隐藏加载提示
    // shows.value = false;
    // await uni.showToast({title: res.msg, icon: 'none'});
    await uToastRef.value.show({message: res.msg, type: 'error', duration: 1000, position: "top"});
  }

}
const reg = () => {
  uni.navigateTo({url: '/pages/register/register'});
  // uni.showToast({title: '注册跳转', icon: 'none'});
}
const zfbLogin = () => {
  uni.showToast({title: '支付宝登录', icon: 'none'});
}
</script>
<style>
.txt {
  font-size: 32rpx;
  font-weight: bold;
  color: #333333;
}

.img-a {
  width: 100%;
  height: 450rpx;
  background-image: url(https://zhoukaiwen.com/img/loginImg/head.png);
  background-size: 100%;
}

.reg {
  font-size: 28rpx;
  color: #fff;
  height: 90rpx;
  line-height: 90rpx;
  border-radius: 50rpx;
  font-weight: bold;
  background: #f5f6fa;
  color: #000000;
  text-align: center;
  margin-top: 30rpx;
}

.login-view {
  width: 100%;
  position: relative;
  margin-top: -120rpx;
  background-color: #ffffff;
  border-radius: 8% 8% 0% 0;
}

.t-login {
  width: 600rpx;
  margin: 0 auto;
  font-size: 28rpx;
  padding-top: 80rpx;
}

.t-login button {
  font-size: 28rpx;
  background: #2796f2;
  color: #fff;
  height: 90rpx;
  line-height: 90rpx;
  border-radius: 50rpx;
  font-weight: bold;
}

.t-login input {
  height: 90rpx;
  line-height: 90rpx;
  margin-bottom: 50rpx;
  border-bottom: 1px solid #e9e9e9;
  font-size: 28rpx;
}

.t-login .t-a {
  position: relative;
}

.t-b {
  text-align: left;
  font-size: 42rpx;
  color: #ffffff;
  padding: 130rpx 0 0 70rpx;
  font-weight: bold;
  line-height: 70rpx;
}

.t-login .t-c {
  position: absolute;
  right: 22rpx;
  top: 22rpx;
  background: #5677fc;
  color: #fff;
  font-size: 24rpx;
  border-radius: 50rpx;
  height: 50rpx;
  line-height: 50rpx;
  padding: 0 25rpx;
}

.t-login .t-d {
  text-align: center;
  color: #999;
  margin: 80rpx 0;
}

.t-login .t-e {
  text-align: center;
  width: 250rpx;
  margin: 80rpx auto 0;
}

.t-login .t-g {
  float: left;
  width: 50%;
}

.t-login .t-e image {
  width: 50rpx;
  height: 50rpx;
}

.t-login .t-f {
  text-align: center;
  margin: 150rpx 0 0 0;
  color: #666;
}

.t-login .t-f text {
  margin-left: 20rpx;
  color: #aaaaaa;
  font-size: 27rpx;
}

.t-login .uni-input-placeholder {
  color: #aeaeae;
}

.cl {
  zoom: 1;
}

.cl:after {
  clear: both;
  display: block;
  visibility: hidden;
  height: 0;
  content: '\20';
}
</style>